package com.web.work02_27Servlet;

import com.web.JDBCUtils;
import com.web.servlet.HelloServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateStudentByid")
public class UpdateStudentByid extends HelloServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置内容类型
        response.setContentType("application/json;charset=UTF-8");
        //接收学生信息
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String gender = request.getParameter("gender");
        String address = request.getParameter("address");
        String idcard = request.getParameter("idcard");
        //修改学生信息
        if (id != null && name != null && age != null && gender != null && address != null && idcard != null){
            //当信息完全时，可以修改数据
            if (JDBCUtils.update("update student set name=?,age=?,gender=?,address=?,idcard=? where id=?", name, Integer.parseInt(age), gender, address, idcard, Integer.parseInt(id)) == 1){
                //修改成功
                response.getWriter().write("msg:修改成功！");
            }else {
                //修改失败
                response.getWriter().write("msg:修改失败！");
            }
        }else {
            //信息不完全，不能修改数据
            response.getWriter().write("msg:信息不完整！");
        }

    }
}